package end.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import end.bean.shiro.Manager;
import end.bean.shiro.Resource;
import end.bean.shiro.Role;
import end.handler.aop.log.EnumLog;
import end.handler.aop.log.SystemControllerLog;
import end.ser.RoleSER;
import end.util.ResourceTree;
import end.util.RightsHelper;

/**
 * 
 * ManagerForController.java 2016-6-24
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Controller
@RequestMapping("/role/")
public class RoleForController {
	private final static Logger logger = LogManager.getLogger(RoleForController.class);

	@Autowired
	private RoleSER<Role> roleSER;

	public RoleSER<Role> getRoleSER() {
		return roleSER;
	}

	public void setRoleSER(RoleSER<Role> roleSER) {
		this.roleSER = roleSER;
	}

	@RequestMapping("/insert")
	@SystemControllerLog(description = "添加一个角色", logType = EnumLog.roleTYPE, logOperState = EnumLog.insertRoleLOG, clazz = Role.class)
	public @ResponseBody
	boolean insertRole(Role role) throws Exception {
		roleSER.insert(role);
		return true;
	}

	@RequestMapping("queryRolesByPage")
	public @ResponseBody
	Map<String, Object> queryRolesByPage(Integer page, Integer rows,
			String sort, String order) throws Exception {
		Role role = new Role();
		role.getPageUtil().setPaging(true);
		role.getPageUtil().setOrderByCondition(sort);
		role.getPageUtil().setQueryCondition(order);
		role.getPageUtil().setPageOffset((page - 1) * rows);
		role.getPageUtil().setPageSize(rows);
		role.setUseStatus('0');
		Map<String, Object> rets = new HashMap<String, Object>();
		List<Role> roles = roleSER.selectByBean(role);
		rets.put("total", roleSER.selectByBeanCount(role));
		rets.put("rows", roles);
		return rets;
	}

	// 查询管理员可用的角色资源
	@RequestMapping(value = "/queryRoleRightsByManager", method = { RequestMethod.POST })
	public @ResponseBody
	List<Object> queryRoleRightsByManager(Manager manager) throws Exception {
		Integer[] rightsID = roleSER.queryRoleRightsByManager(manager.getId());
		List<Role> roles = roleSER.selectByBean(new Role());
		List<Resource> res = new ArrayList<Resource>();
		for (Role role : roles) {
			Resource rs = new Resource();
			rs.setCode(role.getCode());
			rs.setName(role.getName());
			rs.setId(role.getId());
			rs.setType("roleType");
			rs.setParentId(0);
			rs.setChecked(RightsHelper.testRights(
					RightsHelper.sumRights(rightsID), role.getId()));
			res.add(rs);
		}
		return new ResourceTree().menuList(res);
	}

	// 查询管理员可用的角色资源
	@RequestMapping(value = "/drop", method = { RequestMethod.POST })
	@SystemControllerLog(description = "删除角色", logType = EnumLog.roleTYPE, logOperState = EnumLog.deleteRoleLOG, clazz = Role.class)
	public @ResponseBody
	boolean dropRoles(Integer[] drops, Role role) throws Exception {
		roleSER.deleteByPrimaryKey(drops);
		return true;
	}

	@RequestMapping(value = "/saveAuthByManager", method = { RequestMethod.POST })
	@SystemControllerLog(description = "更改管理员的角色", logType = EnumLog.managerTYPE, logOperState = EnumLog.updateManagerRightsLOG, clazz = Manager.class)
	public @ResponseBody
	boolean saveAuthByManager(Integer[] rolesID, Manager manager)
			throws Exception {
		roleSER.saveRoleAuthByManager(rolesID, manager.getId());
		return false;
	}

	@RequestMapping("/isRoleCodeAlreadyUsed")
	public @ResponseBody
	boolean isRoleCodeAlreadyUsed(Role role) {
		return roleSER.isRoleCodeAlreadyUsed(role);
	}

	@RequestMapping("/modifyRole")
	@SystemControllerLog(description = "修改角色信息", logType = EnumLog.roleTYPE, logOperState = EnumLog.updateRoleLOG, clazz = Role.class)
	public @ResponseBody
	boolean modifyRoleByID(Role role) {
		return roleSER.modifyRole(role);
	}
}
